package com.example.login;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.login.db.UserDbHelper;
import com.example.login.entity.UserInfo;

public class LoginActivity extends AppCompatActivity {

    private EditText username;
    private EditText password;

    private CheckBox checkBox;

    private Boolean is_login;

    private SharedPreferences msharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_login);

        // 获取msharedPreferences
        msharedPreferences = getSharedPreferences("user", MODE_PRIVATE);

        // 初始化控件
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        checkBox = findViewById(R.id.checkbox);

        // 注册点击事件
        findViewById(R.id.register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转注册页面
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });

        findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = username.getText().toString();
                String psw = password.getText().toString();

                if (TextUtils.isEmpty(name) || TextUtils.isEmpty(psw)) {
                    Toast.makeText(LoginActivity.this, "请输入用户名密码", Toast.LENGTH_SHORT).show();
                } else {
//                    String username = msharedPreferences.getString("name", null);
//                    String password = msharedPreferences.getString("psw", null);

                    UserInfo login = UserDbHelper.getInstance(LoginActivity.this).login(name);
                    if (login != null && name.equals(login.getUsername()) && psw.equals(login.getPassword())) {
                        // 登录成功
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                    } else {
                        Toast.makeText(LoginActivity.this, "用户密码错误", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_login = isChecked;
            }
        });

    }
}